package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.media.tv.TvContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.GsonBean.SenseBean;
import com.lenovo.smarttraffic.bean.LitepalBean.SenseDBBean;

import java.util.List;

/**
 * Created by Caleb.F
 * on 2020-04-10
 * at 14:25
 */
public class MyListAdapter extends BaseAdapter {
    private Context mContext;
    private List<SenseDBBean> mlist;

    public MyListAdapter(Context context, List<SenseDBBean> list) {
        mContext = context;
        mlist = list;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext)
                    .inflate(R.layout.list_view_item, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }
        instantiationView(mlist.get(position),(ViewHolder)convertView.getTag());
        return convertView;
    }

    private void instantiationView(SenseDBBean senseBean, ViewHolder tag) {
        tag.mTvCo2.setText(senseBean.getCo2()+"");
        tag.mTvHum.setText(senseBean.getHumidity()+"");
        tag.mTvPm.setText(senseBean.getPm()+"");
        tag.mTvSun.setText(senseBean.getLightIntensity()+"");
        tag.mTvTem.setText(senseBean.getTemperature()+"");
        tag.mTvTime.setText(senseBean.getStorageTime());
    }


    class ViewHolder {
        private TextView mTvCo2;
        private TextView mTvHum;
        private TextView mTvTem;
        private TextView mTvPm;
        private TextView mTvSun;
        private TextView mTvTime;
        public ViewHolder(View view) {
            mTvCo2 = view.findViewById(R.id.tv_co2);
            mTvHum = view.findViewById(R.id.tv_hum);
            mTvTem = view.findViewById(R.id.tv_tem);
            mTvPm = view.findViewById(R.id.tv_pm);
            mTvSun = view.findViewById(R.id.tv_sun);
            mTvTime=view.findViewById(R.id.tv_time);
        }
    }

    public void refreshData(List<SenseDBBean> list){
        mlist=list;
        notifyDataSetChanged();
    }

}
